#!/usr/bin/perl
#
use strict;
use warnings;

use Tk;
use Tkad::Handler;
use Tkad::Draw_lines;
use Tkad::Icons;


my %drawing = (
	points   =>   [ ],
	lines    =>   [ ],
);

my $handler = new Tkad::Handler; # start with a default null handler

# set up GUI

my $mw = new MainWindow();

# load icons
my $draw_line_icon = $mw -> Pixmap(
	-data => $Tkad::Icons::draw_line_icon_data,
);

my $can = $mw -> Canvas(
	-width  => 600,
	-height => 400,
);

$can -> Tk::bind('<1>' => [
	\&canvas_click,
	1,
        Ev('x'),
	Ev('y'),
        $handler,
	]
);

$can -> Tk::bind('<2>' => [
	\&canvas_click,
	2,
        Ev('x'),
	Ev('y'),
        $handler,
	]
);

$can -> Tk::bind('<3>' => [
	\&canvas_click,
	3,
        Ev('x'),
	Ev('y'),
        $handler,
	]
);



$can -> pack ( -side => 'right');

my $left_panel = $mw -> Frame();


my $draw_line_button = $left_panel -> Button (
	-image   => $draw_line_icon,
	-command => [\&draw_line_command, $handler],
);

$draw_line_button -> grid (
	-column => 0,
	-row    => 0,
);

$left_panel -> pack (
	-side    => 'left',
	-anchor  => 'n',);

MainLoop();



# CALLBACKS

sub draw_line_command {
	$handler = new Tkad::Draw_lines(\%drawing);
	print("Start drawing...\n");
	print("Handler = ", $handler, "\n");
	return;
}


sub canvas_click {
	my $can     = shift;
	my $ibutton = shift;
	my $x       = shift;
	my $y       = shift;
	my $handler = shift;
	
	print("Handler = ", $handler, "\n");
	$handler = $handler -> handle($can, $ibutton, $x, $y);

	return;
}

